/*******************************************************************************
 * Copyright (c) 2005, 2014 springside.github.io
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *******************************************************************************/
package com.yihu.wlyy.repository;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;

import com.yihu.wlyy.entity.patient.PatientMedicalRecords;

public interface PatientMedicalRecordsDao extends PagingAndSortingRepository<PatientMedicalRecords, Long>, JpaSpecificationExecutor<PatientMedicalRecords> {

	@Query("SELECT a.id, a.code, a.title, a.symptoms, a.images, b.partName, b.content, a.czrq FROM PatientMedicalRecords a, PatientMedicalRecordsItem b WHERE a.code = b.record AND a.doctor = ?1 AND a.patient = ?2 AND a.type = 2 AND a.del = '1'")
	Page<Object> findPatientPartsByDoctor(String doctor, String patient, Pageable pageRequest);
	
	@Query("SELECT a.id, a.code, a.title, a.symptoms, a.images, b.partName, b.content, a.czrq FROM PatientMedicalRecords a, PatientMedicalRecordsItem b WHERE a.code = b.record AND a.doctor = ?1 AND a.patient = ?2 and a.id < ?3 AND a.type = 2 AND a.del = '1'")
	Page<Object> findPatientPartsByDoctor(String doctor, String patient, long id, Pageable pageRequest);
	
//	@Query("SELECT a.id, a.code, a.title, a.symptoms, a.images, b.partName, b.content, a.czrq FROM PatientMedicalRecords a, PatientMedicalRecordsItem b WHERE a.code = b.record AND a.patient = ?1 AND a.type = 2 AND a.del = '1'")
//	Page<Object> findPartsByPatient(String patient, Pageable pageRequest);
//	
//	@Query("SELECT a.id, a.code, a.title, a.symptoms, a.images, b.partName, b.content, a.czrq FROM PatientMedicalRecords a, PatientMedicalRecordsItem b WHERE a.code = b.record AND a.patient = ?1 and a.id < ?1 AND a.type = 2 AND a.del = '1'")
//	Page<Object> findPartsByPatient(String patient, long id, Pageable pageRequest);

	@Modifying
	@Query("update PatientMedicalRecords a set a.del = '0' where a.code = ?1")
	int deleteByCode(String code);
	
	PatientMedicalRecords findByCode(String record);
}
